package wwk.com.android_meilishuo;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wwk.core.utils.FragmentHelper;

import java.util.ArrayList;
import java.util.List;

import wwk.com.android_meilishuo.ui.assort.FragmentAssort;
import wwk.com.android_meilishuo.ui.cart.FragmentCart;
import wwk.com.android_meilishuo.ui.home.FragmentHome;
import wwk.com.android_meilishuo.ui.mine.FragmentMine;
import wwk.com.android_meilishuo.ui.secondfloor.SecondFloorActivity;

public class MainActivity extends AppCompatActivity {

    private Context mContext = this;
    private ImageView imageView_head_search,imageView_sort_search,imageView_me_setting
            ,imageView_home_notice;
    private TextView textView_head_title;

    private RadioGroup radioGroup_main;
    private List<Fragment> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        clickView();
    }


    private void initView() {
        radioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
        imageView_head_search= (ImageView) findViewById(R.id.imageView_home_search);
        imageView_sort_search= (ImageView) findViewById(R.id.imageView_sort_search);
        imageView_me_setting= (ImageView) findViewById(R.id.imageView_me_setting);
        imageView_home_notice= (ImageView) findViewById(R.id.imageView_home_notice);
        textView_head_title= (TextView) findViewById(R.id.textView_head_title);

    }

    private void initFragment() {
        FragmentHome fragment_home = FragmentHome.newInstance();
        mList.add(fragment_home);
        FragmentAssort fragment_assort = FragmentAssort.newInstance();
        mList.add(fragment_assort);
        FragmentCart fragment_cart = FragmentCart.newInstance();
        mList.add(fragment_cart);
        FragmentMine fragmnet_mine = FragmentMine.newInstance();
        mList.add(fragmnet_mine);
        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 0, R.id.frameLayout_main, 0, 0);
        imageView_head_search.setVisibility(View.VISIBLE);
        textView_head_title.setText("美丽说");

    }

    private void clickView() {
        radioGroup_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checked) {
                switch (checked) {
                    case R.id.radioButton_home:
                        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 0, R.id.frameLayout_main, 0, 0);
                        imageView_head_search.setVisibility(View.VISIBLE);
                        imageView_sort_search.setVisibility(View.GONE);
                        imageView_me_setting.setVisibility(View.GONE);
                        imageView_home_notice.setVisibility(View.VISIBLE);
                        textView_head_title.setText("美丽说");
                        break;
                    case R.id.radioButton_assort:
                        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 1, R.id.frameLayout_main, 0, 0);
                        imageView_sort_search.setVisibility(View.VISIBLE);
                        imageView_head_search.setVisibility(View.GONE);
                        imageView_me_setting.setVisibility(View.GONE);
                        imageView_home_notice.setVisibility(View.VISIBLE);
                        textView_head_title.setText("分类");
                        break;
                    case R.id.radioButton_cart:
                        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 2, R.id.frameLayout_main, 0, 0);
                        imageView_me_setting.setVisibility(View.GONE);
                        imageView_home_notice.setVisibility(View.INVISIBLE);
                        imageView_sort_search.setVisibility(View.GONE);
                        imageView_head_search.setVisibility(View.GONE);
                        textView_head_title.setText("购物车");
                        break;
                    case R.id.radioButton_mine:
                        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 3, R.id.frameLayout_main, 0, 0);
                        imageView_me_setting.setVisibility(View.VISIBLE);
                        imageView_sort_search.setVisibility(View.GONE);
                        imageView_head_search.setVisibility(View.GONE);
                        imageView_home_notice.setVisibility(View.VISIBLE);
                        textView_head_title.setText("");
                        break;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_sf, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_sf:
                Intent intent = new Intent(mContext, SecondFloorActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
